package com.wang.controller;

import com.wang.domain.*;
import com.wang.service.Impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class APIController {

    @Autowired
    ExpertServiceImpl _service;

    @Autowired
    ExpertTypeServiceImpl _expertTypeService;

    @Autowired
    ExpertMajorServiceImpl _expertMajorService;




    /**
     * 获取专家属性列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/experttypes")
    public Page<ExpertTypeEntity> getExpertTypes() {
        return _expertTypeService.findAll();
    }

    /**
     * 新增或编辑或删除专家属性
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/experttypes/md", method = RequestMethod.POST)
    public ExpertTypeEntity AddExpertType(@RequestParam String action , @RequestBody ExpertTypeEntity type) {
        switch (action) {
            case "add":
                return _expertTypeService.add(type);
            case "edit":
                return _expertTypeService.edit(type);
            case "delete":
                 _expertTypeService.delete(type);
                 return type;
            default:
                return type;
        }
    }


    /**
     * 获取专家专业列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/expertmajors")
    public Page<ExpertMajorEntity> getExpertMajors() {
        return _expertMajorService.findAll();
    }

    /**
     * 新增或编辑或删除专家专业
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/expertmajors/md", method = RequestMethod.POST)
    public ExpertMajorEntity AddExpertType(@RequestParam String action , @RequestBody ExpertMajorEntity major) {
        switch (action) {
            case "add":
                return _expertMajorService.add(major);
            case "edit":
                return _expertMajorService.edit(major);
            case "delete":
                _expertMajorService.delete(major);
                return major;
            default:
                return major;
        }
    }


    /**
     * 获取专家列表(分页)
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/experts")
    public Page<ExpertEntity> getExperts(@RequestParam int pageNo, @RequestParam int pageSize) {
        return _service.findAll(pageNo, pageSize);
    }

    /**
     * 新增或编辑或删除专家
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/experts/md", method = RequestMethod.POST)
    public ExpertEntity AddExpert(@RequestParam String action , @RequestBody ExpertEntity expert) {
        switch (action) {
            case "add":
                return _service.add(expert);
            case "edit":
                return _service.edit(expert);
            case "delete":
                _service.delete(expert);
                return expert;
            default:
                return expert;
        }
    }

    @ResponseBody
    @RequestMapping("/api/experts/random")
    public List<ExpertEntity> random(@RequestParam int type, @RequestParam int limit) {
        return _service.selectRandoms(type, limit);
    }
}
